﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;
using MySql.Data.MySqlClient;
using System.Data;
namespace BusinessFacade
{
    public class Busi_med_persongroupmanage
    {
        public Busi_med_persongroupmanage()
        {
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public bool Add(med_persongroupmanage_Model model)
        {
            int rowsAffected;
            MySqlParameter[] parameters = {
                                               
                   	new MySqlParameter("pgroupid", MySqlDbType.Int32,200),
					new MySqlParameter("ppersonmoney", MySqlDbType.Decimal,50),
					new MySqlParameter("ppersonage", MySqlDbType.Int32,10),
					new MySqlParameter("ppersonsex", MySqlDbType.VarChar,10),
					new MySqlParameter("ppaymentterm", MySqlDbType.Int32,10),
					new MySqlParameter("parrearageterm", MySqlDbType.Int32,10),
					new MySqlParameter("ppaymentbase", MySqlDbType.Decimal,50),
					new MySqlParameter("parea", MySqlDbType.Int32,10),
					new MySqlParameter("psubordinateunits", MySqlDbType.VarChar,50),
					new MySqlParameter("ppersonstate", MySqlDbType.VarChar,50)};

            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.PersonMoney;
            parameters[2].Value = model.PersonAge;
            parameters[3].Value = model.PersonSex;
            parameters[4].Value = model.PaymentTerm;
            parameters[5].Value = model.ArrearageTerm;
            parameters[6].Value = model.PaymentBase;
            parameters[7].Value = model.Area;
            parameters[8].Value = model.SubordinateUnits;
            parameters[9].Value = model.PersonState;

            DbHelperMySql.RunProcedure("med_persongroupmanage_ADD", parameters);
            rowsAffected = int.Parse(parameters[0].Value.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="model"></param>
        public void Update(med_persongroupmanage_Model model)
        {
            MySqlParameter[] parameters = {
				
                    new MySqlParameter("pgroupid", MySqlDbType.Int32,200),
					new MySqlParameter("ppersonmoney", MySqlDbType.Decimal,50),
					new MySqlParameter("ppersonage", MySqlDbType.Int32,10),
					new MySqlParameter("ppersonsex", MySqlDbType.VarChar,10),
					new MySqlParameter("ppaymentterm", MySqlDbType.Int32,10),
					new MySqlParameter("parrearageterm", MySqlDbType.Int32,10),
					new MySqlParameter("ppaymentbase", MySqlDbType.Decimal,50),
					new MySqlParameter("parea", MySqlDbType.Int32,10),
					new MySqlParameter("psubordinateunits", MySqlDbType.VarChar,50),
					new MySqlParameter("ppersonstate", MySqlDbType.VarChar,50)};

            parameters[0].Value = model.GroupId;
            parameters[1].Value = model.PersonMoney;
            parameters[2].Value = model.PersonAge;
            parameters[3].Value = model.PersonSex;
            parameters[4].Value = model.PaymentTerm;
            parameters[5].Value = model.ArrearageTerm;
            parameters[6].Value = model.PaymentBase;
            parameters[7].Value = model.Area;
            parameters[8].Value = model.SubordinateUnits;
            parameters[9].Value = model.PersonState;


            DbHelperMySql.RunProcedure("med_persongroupmanage_Update", parameters);
        }



        public DataTable GetList(string PersonState, int Area)
        {
            DataSet ds = new DataSet();
            MySqlParameter[] parameters = {
                    new MySqlParameter("personstate", MySqlDbType.VarChar,50),
					new MySqlParameter("area", MySqlDbType.Int32,5)};
            parameters[0].Value = PersonState;
            parameters[1].Value = Area;
            ds = DbHelperMySql.RunProcedure("med_persongroupmanage_Select", parameters);
            return ds.Tables[0];
        }



        public med_persongroupmanage_Model GetListModel(int id)
        {
            med_persongroupmanage_Model model = new med_persongroupmanage_Model();
            string sql = "select * from med_persongroupmanage where groupid=" + id;
            DataSet ds = DbHelperMySql.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["groupid"] != null && ds.Tables[0].Rows[0]["groupid"].ToString() != "")
                {
                    model.GroupId = int.Parse(ds.Tables[0].Rows[0]["groupid"].ToString());
                }



                if (ds.Tables[0].Rows[0]["personmoney"] != null && ds.Tables[0].Rows[0]["personmoney"].ToString() != "")
                {
                    model.PersonMoney = Convert.ToDecimal(ds.Tables[0].Rows[0]["personmoney"].ToString());
                }



                if (ds.Tables[0].Rows[0]["personage"] != null && ds.Tables[0].Rows[0]["personage"].ToString() != "")
                {
                    model.PersonAge = Convert.ToInt32(ds.Tables[0].Rows[0]["personage"].ToString());
                }


                if (ds.Tables[0].Rows[0]["personsex"] != null && ds.Tables[0].Rows[0]["personsex"].ToString() != "")
                {
                    model.PersonSex = ds.Tables[0].Rows[0]["personsex"].ToString();
                }


                if (ds.Tables[0].Rows[0]["paymentterm"] != null && ds.Tables[0].Rows[0]["paymentterm"].ToString() != "")
                {
                    model.PaymentTerm = Convert.ToInt32(ds.Tables[0].Rows[0]["paymentterm"].ToString());
                }

                if (ds.Tables[0].Rows[0]["arrearageterm"] != null && ds.Tables[0].Rows[0]["arrearageterm"].ToString() != "")
                {
                    model.ArrearageTerm = Convert.ToInt32(ds.Tables[0].Rows[0]["arrearageterm"].ToString());
                }

                if (ds.Tables[0].Rows[0]["paymentbase"] != null && ds.Tables[0].Rows[0]["paymentbase"].ToString() != "")
                {
                    model.PaymentBase = Convert.ToDecimal(ds.Tables[0].Rows[0]["paymentbase"].ToString());
                }

                if (ds.Tables[0].Rows[0]["area"] != null && ds.Tables[0].Rows[0]["area"].ToString() != "")
                {
                    model.Area = Convert.ToInt32(ds.Tables[0].Rows[0]["area"].ToString());
                }

                if (ds.Tables[0].Rows[0]["subordinateunits"] != null && ds.Tables[0].Rows[0]["subordinateunits"].ToString() != "")
                {
                    model.SubordinateUnits = ds.Tables[0].Rows[0]["subordinateunits"].ToString();
                }

                if (ds.Tables[0].Rows[0]["personstate"] != null && ds.Tables[0].Rows[0]["personstate"].ToString() != "")
                {
                    model.PersonState = ds.Tables[0].Rows[0]["personstate"].ToString();
                }
                return model;
            }
            model = null;
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            MySqlParameter[] parameters = { 
                             new MySqlParameter("pgroupid",MySqlDbType.Int32)
                                         };
            parameters[0].Value = id;
            DbHelperMySql.RunProcedure("med_persongroupmanage_Delete", parameters);
        }
    }
}
